(function() {
  var _container = document.getElementById('mr');
  var _refreshProcessCode;
  var _rooms;

  var _addRoomRow = function(room) {
    _rooms.add(room, function() {
      deberc.mainRoom.leave();
      deberc.gameRoom.enter(room.id)
    });
  };

  deberc.mainRoom = {
    enter: function() {
      woody.dom.show(_container);
      this.update();
      var that = this;
      _refreshProcessCode = window.setInterval(function() {
        that.update();
      }, deberc.schema.MAIN_ROOM_REFRESH_INTERVAL);
    },
    update:function() {
      deberc.rest.manager.getRooms(this, this.updateCB);
    },
    updateCB:function(response) {
      _rooms.clear();
      for (var i in response.rooms) {
        _addRoomRow.call(this, response.rooms[i]);
      }
    },
    leave:function() {
      window.clearInterval(_refreshProcessCode);
      woody.dom.hide(_container);
    },
    create:function() {
      this.leave();
      deberc.rest.manager.createRoom();
    }
  };
  _rooms = new deberc.view.Table(document.getElementById('mr_t_r'), deberc.mainRoom, deberc.mainRoom.enter);
  new deberc.view.Button('mr_create', deberc.mainRoom, deberc.mainRoom.create);
})();

deberc.mainRoom.enter();
